package com.mc.dao.Impl;

import com.mc.bean.College;
import com.mc.dao.CollegeDao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 学院数据访问实现类
 * @author fyp
 */
public class CollegeDaoImpl extends BaseDao implements CollegeDao {
    @Override
    public List<College> findAll() {
        //创建集合对象
        List<College> list=new ArrayList<>();
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql="select*from college";
            super.getPst(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                //创建对象
                College college=new College(rs.getInt(1),rs.getString(2));
                //将对象放入集合中
                list.add(college);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return list;
    }

    @Override
    public College findById(int colid) {
        College college=null;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "select*from college where colid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,colid);
            //执行预编译指令，得到结果
            rs=pstmt.executeQuery();
            while (rs.next()){
                college=new College(rs.getInt(1),rs.getString(2));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return college;
    }

    @Override
    public int add(College college) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "insert into college(colid,colname) values (?,?)";
            super.getPst(sql);
            super.pstmt.setInt(1,college.getColid());
            super.pstmt.setString(2,college.getColname());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int delete(int colid) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql="delete from college where colid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,colid);

            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int update(College college) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "update college set colname=? where colid=?";
            super.getPst(sql);
            super.pstmt.setString(1,college.getColname());
            super.pstmt.setInt(2,college.getColid());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }
}
